﻿'use strict';
app
    // Login Box controller 
    .controller('IndirectsponsorController', function ($scope, $rootScope, $localStorage, $state, $stateParams, $timeout, CN_TructiepFactory) {
        $scope.nottree = false,
        $scope.progress = false,
        $scope.ShowAssignForm = false,
        $scope.AlertType = "warning",
        $scope.UserAssign = {
            Sponsor: '', Sponsor2: '',UserName: '', Email: '', Password: '', Password2: '', SDTNHARIENG: '',
        }, 
        $scope.downline = {
            UserName: $localStorage.user.UserName,
            HoTen: $localStorage.user.HoTen,
            TotalF1:"0",
            Invest: "0",
            Total: "0",
            TotalPin: "0"
        },
        $scope.ToAssign = function (downline) {                    
            $scope.UserAssign.Sponsor2 = downline.UserName;
            $scope.downlineId = downline.downlineId;
            $scope.ShowAssignForm = true;
        },
        $scope.SaveAssignUser = function (UserAssign) {
            $scope.progress = true;            
            doAssignUser($scope.downlineId, UserAssign)
        }
        $scope.doLoadTree = function () {
            getTree();
        }
        getTree();
        function getTree() {
            $('#tree')
                .on('changed.jstree', function (e, data) {                                        
                    var i, j, r = [];
                    for (i = 0, j = data.selected.length; i < j; i++) {                       
                        $scope.downline = data.instance.get_node(data.selected[i]).data;
                    }
                })
                .jstree({
                    'core': {
                        'data': {
                            'url': function (node) {                            
                                return node.id === '#' ?
                                  $rootScope.GianTiepAPI + '/getRootTree' :
                                  $rootScope.GianTiepAPI + '/GetTreeChildren';
                            },
                            'data': function (node) {
                                if (node.id === "#") {                                
                                    return { 'id': $localStorage.user.UserName };
                                }
                                return { 'id': node.id };
                            },
                        },                    
                        'strings' : {
                            'Loading ...' : 'Please wait loading node data...'
                        },                   
                    }
                });
        };
        function doAssignUser(ChiDinhId, UserAssign) {
            if (ChiDinhId>0) {
                //assign User
                CN_TructiepFactory.onUserAdded(ChiDinhId, UserAssign).then(function (response) {
                    $scope.progress = false;
                    if (response.data.Status) {
                        $scope.AlertType = "success";
                        $scope.messeger = response.data.Messeger;
                        console.log($scope.AlertType);
                        $timeout(function () {
                            $state.reload();
                        }, 3600);
                    } else {
                        $scope.messeger = response.data.Messeger;                        
                    }
                }, function (error) {
                    $scope.progress = false;
                    $scope.messeger = 'Unable to load profile data: ' + error.message;                    
                });
            } else {
                $scope.progress = false;
                $scope.messeger = "Not exist Sponsor2";
                
            }



        }
    })